<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
  <link rel="stylesheet" title="Default" href="http://acm.math.spbu.ru/~sk1/colorer/my.css">

  <script src="http://acm.math.spbu.ru/~sk1/colorer/highlight.js"></script>
  <script src="http://acm.math.spbu.ru/~sk1/colorer/cpp.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
</head>

<body>

<pre><code>
/**
 * Author: Sergey Kopeliovich (Burunduk30@gmail.com)
 * Date: 2013.03.20
 * MaxTest: ??
 */

#include &lt;cstdio&gt;
#include &lt;cstring&gt;
#include &lt;cassert&gt;
#include &lt;algorithm&gt;
#include &lt;iostream&gt;

using namespace std;

#define forn(i, n) for (int i = 0; i &lt; (int)(n); i++)

typedef long long ll;

const int maxN = 500;
const int maxE = 20002;

int n, m, e = 2, head[maxN], to[maxE], c[maxE], f[maxE], next[maxE];
int S, T, h[maxN];
ll ex[maxN];
int qst, qen, q[maxN];
int run, cc, used[maxN];

void addE( int a, int b, int x )
{
  to[e] = b, f[e] = 0, c[e] = x, next[e] = head[a], head[a] = e++;
}

void ReLabel()
{
  cc++, qst = qen = 0, run = 0;
  forn(tt, 2)
  {
    int v = (tt ? S : T);
    used[v] = cc, q[qen++] = v;
    while (qst &lt; qen)
    {
      int x, v = q[qst++];
      if (v != S && v != T && ex[v] &gt; 0)
        run = 1;
      for (int e = head[v]; e; e = next[e])
        if (used[x = to[e]] != cc && x != S && f[e ^ 1] &lt; c[e ^ 1])
          h[x] = h[v] + 1, used[x] = cc, q[qen++] = x;
    }
  }
} 

int main()
{
  #define NAME "flow2"
  assert(freopen(NAME ".in", "r", stdin));
  assert(freopen(NAME ".out", "w", stdout));

  scanf("%d%d", &n, &m);
  forn(i, m)
  {
    int a, b, x;
    scanf("%d%d%d", &a, &b, &x);
    a--, b--;
    addE(a, b, x);
    addE(b, a, 0);
  }

  S = 0, T = n - 1, h[S] = n;
  for (int e = head[S]; e; e = next[e])
    ex[to[e]] += c[e], f[e ^ 1] -= c[e], f[e] += c[e];
  ReLabel();
  while (run)
  {
    while (qen--)
    {
      int u, v = q[qen];
      for (int e = head[v]; e && ex[v]; e = next[e])
        if (f[e] &lt; c[e] && h[u = to[e]] + 1 == h[v])
        {
          int x = min((ll)c[e] - f[e], ex[v]);
          f[e] += x, f[e ^ 1] -= x;
          ex[u] += x, ex[v] -= x;
        }
    }
    ReLabel();
  }
  cout &lt;&lt; ex[T] &lt;&lt; endl;
  forn(i, m)
    printf("%d\n", f[2 * i + 2]);
  return 0;
}

</code></pre>

</body>
</html>

<font style="visibility:hidden">
